package cn.edu.swpu.cins.u_chain.frontend.service.user.Impl;

import cn.edu.swpu.cins.u_chain.Exception.user.TeacherException;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.TeacherDao;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.UserDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.UpdateRole;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.teacher.TeacherDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.teacher.TeacherInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.LocationInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.teacher.TeacherInfoDB;
import cn.edu.swpu.cins.u_chain.frontend.enums.UserRole;
import cn.edu.swpu.cins.u_chain.frontend.service.user.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Service
public class TeacherServiceImpl implements TeacherService{

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class,TeacherException.class,SQLException.class})
    public int addTeacherInfo(TeacherInfo teacherInfo) {
        TeacherInfoDB teacherInfoDB = new TeacherInfoDB(teacherInfo);
        teacherDao.addTeacherInfo(teacherInfoDB);
        return userDao.updateRole(new UpdateRole(teacherInfo.getUserId(), UserRole.TEACHER.getRole()));

    }

    @Override
    public TeacherDetail selectDetail(int userId) {
        return teacherDao.selectTeacherInfo(userId);
    }

}
